package wangjg.test.lucene;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * This class demonstrate the process of creating index with Lucene for text
 * files
 */
public class TxtFileIndexer {
	
	public static void main(String[] args) throws Exception {
		File indexDir = new File("E:\\temp\\index");
		File dataDir = new File("E:\\temp\\text");
		
//		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
		Analyzer analyzer = new IKAnalyzer();
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, analyzer);
		iwc.setOpenMode(OpenMode.CREATE);

		Directory dir = FSDirectory.open(indexDir);
		IndexWriter indexWriter = new IndexWriter(dir, iwc);

		File[] dataFiles = dataDir.listFiles();
		for (int i = 0; i < dataFiles.length; i++) {
			File file = dataFiles[i];
			if (!file.isFile() || !file.getName().endsWith(".java")){
				continue;
			}
			
			Document document = new Document();

			Field pathField = new Field("path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
			pathField.setIndexOptions(IndexOptions.DOCS_ONLY);
			document.add(pathField);

			Reader reader = new FileReader(file);
			document.add(new Field("contents", reader));
			
			indexWriter.addDocument(document);
		}
		indexWriter.close();

		System.out.println("over.");
	}
}
